namespace ghost {
	/**编写普通场景业务逻辑*/
	export class CommonScene extends SceneBase{
		private _interval:number;
		private _lastTime:number;
		private _lastGoldTime:number;
		private _monsterIDs:string[];
		private _monsterNumArr:number[];
		private _noMonster:boolean;
		// private _gameStartTime:number;
		/**关卡状态 0倒计时 1场景准备 2正式出怪*/
		private _state:number;
		private _bedInd:number;
		public _curBed:Monster;
		public _curDoor:Monster;
		public _curBedId:Number;
		public _curDoorId:Number;
		public bedLvUpBtn:GYScaleSprite;
		public doorLvUpBtn:GYScaleSprite;		
		private _module:Module;		
		private _countComponent:CountComponent;
		private _gameStart:boolean;
		public constructor(sceneMgr:map2D.SceneManagerBase) {
			super(sceneMgr);
			let s= this;
			let scene:SceneManager;
			scene = <SceneManager>s.sceneMgr;
			s._module = scene.module;			
		}
		public dispose():void
		{
			super.dispose();
			let s= this;
			if(s._disposed)return;
			GameDispatcher.instance.off(GameEvent.GOLD_CHANGE, s, s.goldChange);
			GameDispatcher.instance.off(GameEvent.GUIDE_COMPLETE, s, s.guideComplete);
		}
		public init():boolean
		{
			let s= this;
			if(super.init())
			{						
				// let obj = Main.gameConfig.roleConfig[102];
				// let r:Monster = <Monster>s.sceneMgr.createRoleByCfg(null,obj, RoleType.MONSTER2, StsType.STAND, map2D.DirectionType.RIGHT, 300, 400);
				// GameRole.roleDataSet(r, obj);
				// r.changeSmart(AIEnum.NONE);
				s.bedLvUpBtn = new GYScaleSprite;
				s.bedLvUpBtn.bitmapData = Main.instance.getRes(Conf.scene + "lvUp.png");					
				s.bedLvUpBtn.on(Laya.Event.CLICK, s, s.levelUpBed);
				s.doorLvUpBtn = new GYScaleSprite;
				s.doorLvUpBtn.bitmapData = Main.instance.getRes(Conf.scene + "lvUp.png");					
				s.doorLvUpBtn.on(Laya.Event.CLICK, s, s.levelUpDoor);
				s._countComponent = new CountComponent;
				GameDispatcher.instance.on(GameEvent.GOLD_CHANGE, s, s.goldChange);
				GameDispatcher.instance.on(GameEvent.GUIDE_COMPLETE, s, s.guideComplete);
				s._gameStart = false;
			}
			return false;		
		}
		protected guideComplete():void
		{
			let s= this;
			s.sceneMgr.resume();
			if(GuideTip.instance.step == 1 && s.isBedValid())
				GuideTip.instance.addGuide(s._curBed.sleepBtn);
			else if(GuideTip.instance.step == 3 && s._module.callBtn.visible)
				GuideTip.instance.addGuide(s._module.callBtn);
			
			
			else if(GuideTip.instance.step == 7 && s.doorLvUpBtn.parent)
				GuideTip.instance.addGuide(s.doorLvUpBtn);
			else if(GuideTip.instance.step == 9 && s.bedLvUpBtn.parent)
				GuideTip.instance.addGuide(s.bedLvUpBtn);
			else
				GuideTip.instance.addGuide();
		}
		protected goldChange():void
		{
			let s= this;
			if(s._curBed == null)
				return;
			
			let gold:number;
			gold = MyPlayerData.getInstance().gold;
			
			s.bedLvUpBtn.x = s._curBed.skin.x - (s.bedLvUpBtn.width>>1);
			s.bedLvUpBtn.y = s._curBed.skin.y - (s.bedLvUpBtn.height>>1);
			s.doorLvUpBtn.x = s._curDoor.skin.x - (s.doorLvUpBtn.width>>1);
			s.doorLvUpBtn.y = s._curDoor.skin.y - (s.doorLvUpBtn.height>>1);						
			let canLevel:boolean;
			canLevel = s.isBedValid() && gold >= s._curBed.levelUpCost;
			this.showLvUpBtn(s.bedLvUpBtn, canLevel?s._module.roleTopLay:null);
			canLevel = s.isDoorValid() && gold >= s._curDoor.levelUpCost;
			this.showLvUpBtn(s.doorLvUpBtn, canLevel?s._module.roleTopLay:null);			
			if(s.bedLvUpBtn.parent && GuideTip.instance.step == 9)							
				GuideTip.instance.addGuide(s.bedLvUpBtn);			
			else if(s.doorLvUpBtn.parent && GuideTip.instance.step == 7)
				GuideTip.instance.addGuide(s.doorLvUpBtn);
			else 
				GuideTip.instance.addGuide();
		}
		protected showLvUpBtn(btn:GYScaleSprite, pr:Laya.Sprite):void
		{
			let s = this;
			if(pr)
			{	
				pr.addChild(btn);				
			}
			else
			{
				if(btn.parent)
					btn.parent.removeChild(btn)
			}
		}
		public roleDead(r:GameRole, atkData:OwnerData=null, buffData?:ghost.IBuffData):void
		{
			let s = this;			
			if(r == s._curDoor)
			{
				if(s.doorLvUpBtn.parent)
					s.doorLvUpBtn.parent.removeChild(s.doorLvUpBtn);
			}
			else if(r == s._curBed)
			{
				if(s.bedLvUpBtn.parent)
					s.bedLvUpBtn.parent.removeChild(s.bedLvUpBtn);
				let myPlayer:MyPlayer = <MyPlayer>s.sceneMgr.myPlayer;
				myPlayer.setSts(StsType.DEATH);
				s.gameEnd(false);
			}
			else
			{
				let arr:Monster[] = (<SceneManager>s.sceneMgr).getMonsterList2();
				let len:number;
				len = arr.length;
				while(--len>-1)
				{
					if(!arr[len]._isDead)
						return;
				}
				if(s._noMonster)
				{
					s.gameEnd(true);
				}
			}
		}
		public gameEnd(win:boolean):void
		{
			let s = this;
			s._gameStart = false;
			s._curBedId = s._curDoorId = NaN;
			s._curDoor = s._curBed = null;
			if(s._module.callUI)
				s._module.callUI.hide();
			let scene:SceneManager;			
			scene = <SceneManager>s.sceneMgr;
			Laya.timer.once(1000,s, ()=>{
				if(s._disposed)return;
				scene.pause();
				scene.gameEnd(win);
			});		
		}		
		public get gameStart():boolean
		{
			return this._gameStart;
		}
		public start():void
		{
			let levelConfigs:any = Main.gameConfig.levelMonster;
			let levelConfig:any = levelConfigs[Main.curLevelIndex]?levelConfigs[Main.curLevelIndex]:levelConfigs[levelConfigs.length - 1];
			let s = this;
			let t:number;
			t = s.sceneMgr.timeTick;
			s._interval = Main.gameConfig.monsterInterval>0?Main.gameConfig.monsterInterval:6000;
			s._lastTime = t - s._interval;
			s._lastGoldTime = 0;
			s._monsterNumArr = levelConfig.monsterNum.concat();
			s._monsterIDs = Main.gameConfig.levelMonsterIDs;
			s._noMonster = false;
			// s._gameStartTime = t + Main.gameConfig.startCount;
			s._state = 0;
			s.sceneMgr.setMapViewRole(s.sceneMgr.myPlayer);
			s.showAllDoor(false);			
			GuideTip.instance.addGuide();
			s._countComponent.show(s._module, false, false, ()=>{
				s._gameStart = true;
			}, s);		
			if(s._module.dialogUI)	
				s._module.addChild(s._module.dialogUI);
		}
		public levelUpDoor():void
		{
			let s = this;
			if(!s.isDoorValid())
				return;
			let cost:number = s._curDoor.levelUpCost;
			let myData:MyPlayerData = MyPlayerData.getInstance();
			if(myData.gold < cost)
				return;
			s._curDoor.levelUp();
			myData.addGold(-cost);			
		}
		public levelUpBed():void
		{
			let s = this;
			if(!s.isBedValid())
				return;
			let cost:number = s._curBed.levelUpCost;
			let myData:MyPlayerData = MyPlayerData.getInstance();
			if(myData.gold < cost)
				return;
			s._curBed.levelUp();
			myData.addGold(-cost);			
		}
		public levelUpTower(towerIndex:number):void
		{
			let s = this;
			if(s._curBed == null)
				return;
			
			let m:Monster;
			let roleID:string;
			let scene:SceneManager;			
			scene = <SceneManager>s.sceneMgr;
			roleID = Main.gameConfig.towerIDs[towerIndex];
			let cost:number = Main.getTowerLvUpCost(roleID);
			let myData:MyPlayerData = MyPlayerData.getInstance();
			if(myData.gold < cost)
				return;
			
			let arr:Monster[] = scene.findMonsterByRoleID(CampType.CAMP_1, roleID);
			if(arr.length > 0)//已经存在则升级
			{
				m = arr[0];
				m.levelUp();								
			}
			else
			{
				let obj = Main.gameConfig.roleConfig[roleID];								
				let posInd:number = towerIndex*2;
				let x:number,y:number;
				x = Main.gameConfig.towerPos[s._bedInd][posInd];
				y = Main.gameConfig.towerPos[s._bedInd][posInd + 1];
				m = <Monster>s.sceneMgr.createRoleByCfg(null,obj, RoleType.MONSTER, StsType.STAND, map2D.DirectionType.RIGHT, x, y);
				GameRole.roleDataSet(m, obj);
				scene.createEffect(null, RoleType.EFFECT, Main.gameConfig.levelEffectIDs[0], m.absX, m.absY, StsType.STAND, 0, map2D.LayerType.TOP);				
				SoundManager.instance.play(`${Conf.sound}lvup.mp3`, 0, 1,null,null,SoundData.BACKGROUND_SOUND);	
			}
			myData.addGold(-cost);				
		}
		private showAllDoor(val:boolean):void
		{
			let s= this;
			let len:number,roleID:number;
			let arr:Monster[];
			let scene:SceneManager;
			scene = <SceneManager>s.sceneMgr;
			arr = scene.getMonsterList();
			len = arr.length;
			while(--len>-1)
			{				
				if(arr[len].isDoor())
					arr[len].setDisplayVisible(val);
			}
		}		
		public isDoorValid():boolean
		{
			let s = this;
			return s._curDoor && s._curDoor._id == s._curDoorId && !s._curDoor._isDead && !s._curDoor.isDel();
		}
		public isBedValid():boolean
		{
			let s = this;
			return s._curBed && s._curBed._id == s._curBedId && !s._curBed._isDead && !s._curBed.isDel();
		}
		public myPlayerSleep():void
		{
			let s =this;			
			let myPlayer:MyPlayer = <MyPlayer>s.sceneMgr.myPlayer;
			if(myPlayer.sts == StsType.FALL)
				return;
			if(s._state != 0)
				return;			
			++s._state;
			let scene:SceneManager;
			scene = <SceneManager>s.sceneMgr;
			let bed:Monster = scene.findNearMonsterByType(1);
			s._curDoor = scene.findMonsterByRoomInd(2, bed.roomIndex)[0];
			s._curBed = bed;
			s._curBedId = s._curBed._id;
			s._curDoorId = s._curDoor._id;
			myPlayer.setSts(StsType.FALL,map2D.DirectionType.LEFT);				
			myPlayer.absX = bed.absX;
			myPlayer.absY = bed.absY + 33;
			let ind:number;
			let pos:number[];
			s._bedInd = ind = bed.roomIndex;
			pos = Main.gameConfig.bedPos[ind];
			scene.setMapViewRole(null);			
			s.showAllDoor(true);			
			let y:number;
			y = pos[1] + s.sceneMgr.sceneHeight <= s.sceneMgr.mapMaxY?pos[1]:(s.sceneMgr.mapMaxY - s.sceneMgr.sceneHeight);
			if(y + s.sceneMgr.sceneHeight < 1260)
				y = 1260 - s.sceneMgr.sceneHeight;
			scene.viewMoveTo(pos[0], y, 0, 1000,()=>{				
				s.goldChange();
				++s._state;
			},s);			
			s._module.dirBoard.visible = false;			
		}
		public loop(t:number):void
		{
			let s= this;		
			let scene:SceneManager;
			scene = <SceneManager>s.sceneMgr;	
			if(s._state >= 1)
			{
				let gold:number;
				if(s.isBedValid())
				{
					gold = Main.gameConfig.bedLvConfig[s._curBed.level-1].goldPerSec;
					if(t - s._lastGoldTime > 1000)
					{
						GoldTip.show(gold);
						MyPlayerData.getInstance().addGold(gold);
						s._lastGoldTime = t;
					}
				}				
			}
			if(s._gameStart)
			{
				s.myPlayerSleep();
				if(GuideTip.instance.step == 1 && s.sceneMgr.myPlayer.sts == StsType.FALL)
					++GuideTip.instance.step;
				if(s._state == 2)
				{
					let arr:Monster[] = scene.getMonsterList2();
					let firstMonster = arr[0];
					let per:number = Main.gameConfig.monsterHPPercentInterval;
					let flag:boolean;
					if(per > 0)
						flag = (firstMonster == null || arr.length == 1 && firstMonster._hp < firstMonster._hpMax*per)
					else
						flag = t - s._lastTime > s._interval;
					if(flag)//出怪
					{
						s._lastTime = t;
						let i:number,len:number;
						len = s._monsterNumArr.length;
						for(i=0;i<len;++i)
						{
							if(s._monsterNumArr[i] > 0)
							{
								--s._monsterNumArr[i];						
								let obj = Main.gameConfig.roleConfig[s._monsterIDs[i]];
								let r:Monster = <Monster>s.sceneMgr.createRoleByCfg(null,obj, RoleType.MONSTER2, StsType.STAND, map2D.DirectionType.RIGHT, s.sceneMgr.mapX + 360, s.sceneMgr.mapY + 50);
								GameRole.roleDataSet(r, obj);								
								break;
							}
						}
						if(i == len)
						{
							//无怪
							s._noMonster = true;
						}
					}		
				}				
			}
			else
			{
				// let bed:Monster = scene.findNearMonsterByType(1);
				// let dis:number;
				// dis = PositionUtil.calculateDistance(bed.absX, bed.absY, myPlayer.absX, myPlayer.absY);
				
				// if(dis < 100)
				// 	s.myPlayerSleep();
				//等待倒计时
			}
			
		}
		// private showGuide(type:number):void
		// {
		// 	let s= this;			
		// 	if(Main.curLevelIndex == 1)
		// 	{
		// 		if(s._module.dialogUI == null)
		// 			s._module.dialogUI = new DialogUI;
		// 		s._module.dialogUI.show(s._module, true, true, type);
		// 	}
		// }
	}
}